package controller.business.observerAssess;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import itext.ItextFont;
import util.DateUtil;
import wt.entity.business.BusinessInfo;
import wt.entity.business.ObserverAssess;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ObserverAssessPdf {
    public static void createPdf(OutputStream outputStream,java.util.List<ObserverAssess> observerAssessList , BusinessInfo businessInfo) throws DocumentException, IOException {


        //设置纸张大小
        Document document = new Document(PageSize.A3);
        //建立一个书写器，与document对象关联
        PdfWriter.getInstance(document, outputStream);
        document.open();

        Paragraph paragraph = new Paragraph("现场审查工作评价表（观察员）", ItextFont.fangZhengPdf18);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.setLeading(50f);

        document.add(paragraph);


        //设置Table表格,创建一个7列的表格
        PdfPTable table = new PdfPTable(7);
        float width[] = {1, 1.1F, 1, 1, 1, 1, 1};//设置每列宽度比例
        table.setWidths(width);
        table.setWidthPercentage(100);//占页面宽度比例

        PdfPCell[] cell = new PdfPCell[6];
        cell[0] = new PdfPCell(new Phrase("企业名称", ItextFont.fangSongNormalPdf10));
        cell[1] = new PdfPCell(new Phrase( businessInfo.getCompanyName(), ItextFont.fangSongNormalPdf10));
        cell[1].setColspan(2);
        cell[2] = new PdfPCell(new Phrase("产品名称", ItextFont.fangSongNormalPdf10));
        cell[3] = new PdfPCell(new Phrase( businessInfo.getProductName(), ItextFont.fangSongNormalPdf10));
        cell[4] = new PdfPCell(new Phrase("核查时间", ItextFont.fangSongNormalPdf10));
        cell[5] = new PdfPCell(new Phrase(DateUtil.date2String(businessInfo.getEndTime(),"yyyy-MM-dd"), ItextFont.fangSongNormalPdf10));
        addTable(cell,table);

        cell = new PdfPCell[7];
        for (int i = 0;i  <observerAssessList.size() ; i++) {
            ObserverAssess temp=observerAssessList.get(i);
            if ("leader".equals(temp.getInspectorInfo().getExpertRole())) {
                cell[0] = new PdfPCell(new Phrase("审查组长姓名", ItextFont.fangSongBoldPdf10));
                cell[1] = new PdfPCell(new Phrase("审查组长证书编号", ItextFont.fangSongBoldPdf10));
                cell[2] = new PdfPCell(new Phrase("组织协调能力", ItextFont.fangSongBoldPdf10));
                cell[3] = new PdfPCell(new Phrase("审查业务能力", ItextFont.fangSongBoldPdf10));
                cell[4] = new PdfPCell(new Phrase("公平公正性", ItextFont.fangSongBoldPdf10));
                cell[5] = new PdfPCell(new Phrase("廉洁自律", ItextFont.fangSongBoldPdf10));
                cell[6] = new PdfPCell(new Phrase("综合评价", ItextFont.fangSongBoldPdf10));
                addTable(cell, table);
                cell[0] = new PdfPCell(new Phrase(temp.getInspectorInfo().getExpertName(), ItextFont.fangSongNormalPdf10));
                cell[1] = new PdfPCell(new Phrase(temp.getInspectorInfo().getCertificateNum(), ItextFont.fangSongNormalPdf10));
                cell[2] = new PdfPCell(createCellText(temp.getCooperation()));
                cell[3] = new PdfPCell(createCellText(temp.getBusiness()));
                cell[4] = new PdfPCell(createCellText(temp.getFairness()));
                cell[5] = new PdfPCell(createCellText(temp.getHonest()));
                cell[6] = new PdfPCell(createCellText(temp.getComprehensive()));
                addTable(cell, table);
                break;
            }
        }


        boolean flag=false;
        for (int i = 0;i  <observerAssessList.size() ; i++) {
            ObserverAssess temp=observerAssessList.get(i);
            if ("crew".equals(temp.getInspectorInfo().getExpertRole()) && !"C".equals(temp.getInspectorInfo().getCertificateGrade())){
                if (flag==false){
                    cell[0] = new PdfPCell(new Phrase("审查组成员姓名", ItextFont.fangSongBoldPdf10));
                    cell[1] = new PdfPCell(new Phrase("审查员证书编号", ItextFont.fangSongBoldPdf10));
                    cell[2] = new PdfPCell(new Phrase("协调配合能力", ItextFont.fangSongBoldPdf10));
                    cell[3] = new PdfPCell(new Phrase("审查业务能力", ItextFont.fangSongBoldPdf10));
                    cell[4] = new PdfPCell(new Phrase("公平公正性", ItextFont.fangSongBoldPdf10));
                    cell[5] = new PdfPCell(new Phrase("廉洁自律", ItextFont.fangSongBoldPdf10));
                    cell[6] = new PdfPCell(new Phrase("综合评价", ItextFont.fangSongBoldPdf10));
                    addTable(cell,table);
                    flag=true;
                }
                cell[0] = new PdfPCell(new Phrase(temp.getInspectorInfo().getExpertName(), ItextFont.fangSongNormalPdf10));
                cell[1] = new PdfPCell(new Phrase(temp.getInspectorInfo().getCertificateNum(), ItextFont.fangSongNormalPdf10));
                cell[2] = new PdfPCell(createCellText(temp.getCooperation()));
                cell[3] = new PdfPCell(createCellText(temp.getBusiness()));
                cell[4] = new PdfPCell(createCellText(temp.getFairness()));
                cell[5] = new PdfPCell(createCellText(temp.getHonest()));
                cell[6] = new PdfPCell(createCellText(temp.getComprehensive()));


                addTable(cell,table);
            }
        }


        flag=false;
        for (int i = 0;i  <observerAssessList.size() ; i++) {
            ObserverAssess temp=observerAssessList.get(i);
                if ("crew".equals(temp.getInspectorInfo().getExpertRole()) && "C".equals(temp.getInspectorInfo().getCertificateGrade())){
                    if (flag==false){
                    cell[0] = new PdfPCell(new Phrase("实习审查员姓名", ItextFont.fangSongBoldPdf10));
                    cell[1] = new PdfPCell(new Phrase("实习审查员证书编号", ItextFont.fangSongBoldPdf10));
                    cell[2] = new PdfPCell(new Phrase("协调配合能力", ItextFont.fangSongBoldPdf10));
                    cell[3] = new PdfPCell(new Phrase("审查业务能力", ItextFont.fangSongBoldPdf10));
                    cell[4] = new PdfPCell(new Phrase("公平公正性", ItextFont.fangSongBoldPdf10));
                    cell[5] = new PdfPCell(new Phrase("廉洁自律", ItextFont.fangSongBoldPdf10));
                    cell[6] = new PdfPCell(new Phrase("综合评价", ItextFont.fangSongBoldPdf10));
                    addTable(cell,table);
                    flag=true;
                }
                    cell[0] = new PdfPCell(new Phrase(temp.getInspectorInfo().getExpertName(), ItextFont.fangSongNormalPdf10));
                    cell[1] = new PdfPCell(new Phrase(temp.getInspectorInfo().getCertificateNum(), ItextFont.fangSongNormalPdf10));
                    cell[2] = new PdfPCell(createCellText(temp.getCooperation()));
                    cell[3] = new PdfPCell(createCellText(temp.getBusiness()));
                    cell[4] = new PdfPCell(createCellText(temp.getFairness()));
                    cell[5] = new PdfPCell(createCellText(temp.getHonest()));
                    cell[6] = new PdfPCell(createCellText(temp.getComprehensive()));
                addTable(cell,table);
            }
        }


        paragraph = new Paragraph();
        paragraph.setSpacingBefore(5f);
        paragraph.add(table);
        document.add(paragraph);


        paragraph = new Paragraph("观察员（签字）：                             年       月       日\n" +
                "填表说明：1、本表由区县市场监管局观察员填写。 2、“综合评价”的判定原则为：有2项以上(含2项)为“一般”的，综合评价为“一般”；有1项以上(含 1项)为“差”的，综合评价为“差”；其他情况综合评价为“好”。\n" , ItextFont.fangSongNormalPdf10);
        document.add(paragraph);


        document.close();


    }

    public static void  addTable(PdfPCell[] cell ,PdfPTable table){
        for (int i = 0; i < cell.length; i++) {
            cell[i].setHorizontalAlignment(Element.ALIGN_CENTER);//居中显示
            cell[i].setVerticalAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
            cell[i].setPaddingTop(10.5F);
            cell[i].setPaddingBottom(10.5F);
            table.addCell(cell[i]);
        }
    }

    public static Phrase   createCellText(int i){
        Chunk check= new Chunk("☑", ItextFont.fuhaoPdf10);
        Phrase phrase=new Paragraph();
        if ( 0==i){
            Chunk chunk= new Chunk("好□ 一般□ 差", ItextFont.fangSongNormalPdf10);
            phrase.add(chunk);
            phrase.add(check);
        }else if (1==i){
            Chunk chunk1= new Chunk("好□ 一般", ItextFont.fangSongNormalPdf10);
            Chunk chunk2= new Chunk(" 差□", ItextFont.fangSongNormalPdf10);
            phrase.add(chunk1);
            phrase.add(check);
            phrase.add(chunk2);
        }else {
            Chunk chunk1= new Chunk("好", ItextFont.fangSongNormalPdf10);
            Chunk chunk2= new Chunk(" 一般□ 差□", ItextFont.fangSongNormalPdf10);
            phrase.add(chunk1);
            phrase.add(check);
            phrase.add(chunk2);
        }
        return phrase;
    }




    public static void main(String[] args) {
        String file = "d:/1111.pdf";
        java.util.List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();

        Map<String,Object> map=new HashMap<String, Object>();

        map.put("leader",1);
        map.put("grade","AA");
        map.put("expertName","expertName");
        map.put("certificateNum","certificateNum");
        map.put("cooperation",1);
        map.put("business",2);
        map.put("fairness",0);
        map.put("honest",0);
        map.put("comprehensive",0);
        list.add(map);

        map=new HashMap<String, Object>();

        map.put("leader",0);
        map.put("grade","B");
        map.put("expertName","expertName");
        map.put("certificateNum","certificateNum");
        map.put("cooperation",1);
        map.put("business",2);
        map.put("fairness",1);
        map.put("honest",2);
        map.put("comprehensive",1);
        list.add(map);


        map=new HashMap<String, Object>();

        map.put("leader",0);
        map.put("grade","C");
        map.put("expertName","expertName");
        map.put("certificateNum","certificateNum");
        map.put("cooperation",1);
        map.put("business",2);
        map.put("fairness",1);
        map.put("honest",2);
        map.put("comprehensive",1);
        list.add(map);


        map=new HashMap<String, Object>();

        map.put("leader",0);
        map.put("grade","B");
        map.put("expertName","expertName");
        map.put("certificateNum","certificateNum");
        map.put("cooperation",1);
        map.put("business",2);
        map.put("fairness",1);
        map.put("honest",2);
        map.put("comprehensive",1);
        list.add(map);


        map=new HashMap<String, Object>();

        map.put("leader",0);
        map.put("grade","C");
        map.put("expertName","expertName");
        map.put("certificateNum","certificateNum");
        map.put("cooperation",1);
        map.put("business",2);
        map.put("fairness",1);
        map.put("honest",2);
        map.put("comprehensive",1);
        list.add(map);

        Map busMap = new HashMap();
        busMap.put("companyName","comp1anyName");
        busMap.put("productName","productN1ame");
        busMap.put("checkTime",new Date());


    }
}



